#include <iostream>
#include <memory>
#include <string>

// 1、作为桥接实现的color类
class color {
    public:
    virtual ~color() {}
    virtual void FillColor() = 0;
};

// 2.1 实现了color的具体类
class Red : public color {
    public:
    Red():colorType_("RED") {}
    ~Red() {}
    void FillColor()override {
        std::cout << "填充颜色：" << colorType_ << std::endl;
    }   
    private:
    std::string colorType_;
};

// 2.2 实现了color的具体类
class Blue : public color {
    public:
    Blue():colorType_("BLUE") {}
    ~Blue() {}
    void FillColor()override {
        std::cout << "填充颜色：" << colorType_ << std::endl;
    }   
    private:
    std::string colorType_;
};

// 具体实现抽象类，提供接口定义
class Shape {
    public:
    virtual ~Shape() {
        if(m_color_) {
            delete m_color_;
            m_color_ = nullptr;
        }
    }
    virtual void showShape() = 0;
    virtual void setColor(color* color) = 0;
    protected:
    virtual void drawColor() = 0;
    virtual void drawShape() = 0;
    color* m_color_;
};

// 具体实现类，实现接口
class Circle : public Shape {
    public:
    Circle():ShapeType_("Circle") {}
    ~Circle() {}
    void showShape() override {
        drawShape();
        drawColor();
    }
    void setColor(color* color) override {
        if(m_color_) {
            delete m_color_;
            m_color_ = nullptr;
        }
        this->m_color_ = color;
    }
    private:
    void drawColor() override {
        this->m_color_->FillColor();
    }
    void drawShape() override{
        std::cout << "绘制" << ShapeType_ << std::endl;
    }
    std::string ShapeType_;
};

// 具体实现类，实现接口
class Rect : public Shape {
    public:
    Rect():ShapeType_("Rectangle") {}
    ~Rect() {}
    void showShape() override {
        drawShape();
        drawColor();
    }
    void setColor(color* color) override {
        if(m_color_) {
            delete m_color_;
            m_color_ = nullptr;
        }
        this->m_color_ = color;
    }
    private:
    void drawColor() override {
        this->m_color_->FillColor();
    }
    void drawShape() override{
        std::cout << "绘制" << ShapeType_ << std::endl;
    }
    std::string ShapeType_;
};


int main() {
    std::cout << "hello 桥接模式" << std::endl;
    Shape* circle = new Circle();
    circle->setColor(new Red());
    circle->showShape();

    circle->setColor(new Blue());
    circle->showShape();
    delete circle;
    circle = nullptr;


    Shape* rectangle = new Rect();
    rectangle->setColor(new Red());
    rectangle->showShape();

    delete rectangle;
    rectangle = nullptr;
    return 0;
}